﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web.UI;

using RaceList.activity.help;
using RaceList.activity.util;

namespace RaceList.activity.mhalloween
{
    /// <summary>
    /// 我为甚么讨厌aspx就是因为它所有的代码基本上都不能重用
    ///     ?relay=1234(唯一的参数)
    /// </summary>
    public partial class Relayresult : Page, IAuthWeixin
    {
        public Activity_Relay Relay { get; set; }

        public string Content { get; set; }

        public string ChengjiTip { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string relayId = Request.QueryString[Parameters.relay];

                if (string.IsNullOrEmpty(relayId))
                {
                    Response.Redirect404(Request.Url.ToString());
                }

                #region

                Activity_Relay relay = RelayQuery.GetRelay(Convert.ToInt32(relayId));
                if (relay != null)
                {
                    SessionHelp.CycleId = relay.CycleId;
                    Relay = relay;
                }
                else
                {
                    Response.Redirect404(Request.Url.ToString());
                }

                #endregion

                InitProperty();
                InitAwardTip();
                #region 获取中间显示的内容

                Content = GetContent();
                ChengjiTip = GetChengjiTip();

                #endregion
            }
        }

        private string GetChengjiTip()
        {
            var result = string.Empty;

            //从game到result的时候只有有一个score的参数的时候才会弹出提示层这个是不同的

            if (!string.IsNullOrEmpty(Request.QueryString["score"]))
            {
                double score = 0.0;
                double.TryParse(Request.QueryString["score"], out score);
                //如果用户没有提交信息的话，会给一个
                var awardUser = RelayQuery.GetActivity_AwardUser(Relay.Id);
                var headUrl = string.IsNullOrEmpty(SessionHelp.UserData.HeadUrl) ? "images/user.jpg" : SessionHelp.UserData.HeadUrl;
                if (awardUser != null)
                {
                    result = @"<div class='score_box'>
                            <img src='{0}' alt=''>
                            <h4>恭喜您获得<b>{1}</b>分</h4>
                            <p>捣乱小鬼已被赶走，您拯救了北京欢乐谷。</p>
                            <div class='chest_text'>
                                <i></i>
                                <p>人品爆发,获得惊喜宝箱1个,请分享后到领奖中心填写领奖信息。</p>
                            </div>
                        </div>";
                    result = string.Format(result, headUrl, score);
                }
                else
                {
                    result = @"<div class='score_box'>
                            <img src='{0}' alt=''>
                            <h4>恭喜您获得<b>{1}</b>分</h4>
                            <p>捣乱小鬼已被赶走，您拯救了北京欢乐谷。</p>
                        </div>";
                    result = string.Format(result, headUrl, score);
                }
            }
            return result;
        }


        //我实在看不下去在前台写那些判断逻辑了，所以将所有的逻辑全部写到后台来包括两个div的内容的书写
        public string GetContent()
        {
            string result = string.Empty;

            ActivityConfig activityConfig = ActivityConfig.GetActivityConfig();
            string userName = Chengji.NickName.SubHtml(8);
            double sumScore = Chengji.SumScore;
            int mingci = Chengji.Mingci;
            string baifenbi = Chengji.Baifenbi == 0.0 ? "多数" : Chengji.Baifenbi + "%";

            //我也要玩
            string indexUrl = SessionHelp.IndexUrl;
            //重新玩
            string playagainUrl = "Relaygame.aspx?" + Parameters.cycle + "=" + Relay.CycleId + "&" + Parameters.relay +
                                  "=" + Parameters.first;
            //帮他玩
            string helpplayUrl = "Relaygame.aspx?" + Parameters.relay + "=" + Relay.Id;

            string singlepaihangUrl = "Paihangbang.aspx?" + Parameters.userId + "=" + SessionHelp.UserData.WeixinUserId;
            //如果不是分享的用户点击不现实帮他玩，而判断是否是点击的分享的链接（有$s=s）
            bool isshowhelp = false;
            if (Request.QueryString["s"] == "s")
            {
                isshowhelp = true;
            }

            #region

            if (ActivityStatus == ActivityStatus.WeiKaishi)
            {
                if (mingci != 0)
                {
                    result += @"<div class='score_text'>
			                    <p><b>{0}</b>已经获得<b>{1}</b>分</p>
			                    <p>位于第<b>{2}</b>位，打败全国<b>{3}</b>的玩家</p>
			                    <div class='over_event'>本活动未开始，感谢您的参与^_^</div>
		                    </div>";
                    result = string.Format(result, userName, sumScore, mingci, baifenbi);
                }
                else
                {
                    result += @"<div class='score_text'>
			                    <p><b>{0}</b>已经获得<b>{1}</b>分</p>
			                    <div class='over_event'>本活动未开始，感谢您的参与^_^</div>
		                    </div>";
                    result = string.Format(result, userName, sumScore);
                }
            }
            else if (ActivityStatus == ActivityStatus.Jiesu)
            {
                if (mingci != 0)
                {
                    result += @"<div class='score_text'>
			                    <p><b>{0}</b>已经获得<b>{1}</b>分</p>
			                    <p>位于第<b>{2}</b>位，打败全国<b>{3}</b>的玩家</p>
			                    <div class='over_event'>本活动已结束，感谢您的参与^_^</div>
		                    </div>";
                    result = string.Format(result, userName, sumScore, mingci, baifenbi);
                }
                else
                {
                    result += @"<div class='score_text'>
			                    <p><b>{0}</b>已经获得<b>{1}</b>分</p>
			                    <div class='over_event'>本活动已结束，感谢您的参与^_^</div>
		                    </div>";
                    result = string.Format(result, userName, sumScore);
                }
            }
            else if (ActivityStatus == ActivityStatus.Jinxingzhong)
            {
                result += @"<div class='score_text'>
			                <p><b>{0}</b>已经获得<b>{1}</b>分</p>
			                <p>累计分数越高，越有机会获得奖品</p>
		                </div>";
                result = string.Format(result, userName, sumScore);
            }

            #endregion

            #region

            if (Relay.WeixinUserId == SessionHelp.UserData.WeixinUserId)
            {
                if (Chengji.IsGameOver)
                {
                    result += @"<div class='invite_friend'>
	    		                    <a href='{0}' class='playagain_btn'><i></i>手残啦,重玩一次</a>
    		                    </div>";
                    result = string.Format(result, playagainUrl);
                }
                else
                {
                    result += @"<div class='invite_friend'>
			                    <h6>再邀请{0}位好友帮帮玩，获奖机率更高，我读书多，不会骗你！</h6>
			                    <a href='javascript:;' class='friend_btn'>邀请朋友帮帮玩</a>
			                    <a href='{1}' class='playagain_btn'><i></i>手残啦,重玩一次</a>
		                    </div>";
                    int shengyuNum = activityConfig.TotalPerson - 1 - Chengji.Helps.Count;
                    result = string.Format(result, shengyuNum, playagainUrl);
                }
            }
            else
            {
                if (LoginUserData.CanHelp)
                {
                    if (isshowhelp)
                    {
                        result += @"<div class='playtoo_btn'>
			                        <a href='{0}'>帮他玩</a>
			                        <a href='{1}'>我也要玩</a>
		                        </div>";
                        result = string.Format(result, helpplayUrl, indexUrl);
                    }
                    else
                    {
                        result += @"<a href='{0}' class='playtooBtn'>我也要玩</a>";
                        result = string.Format(result, indexUrl);
                    }
                }
                else if (LoginUserData.IsPlay) //现在我改了改成了跳转到所有成绩的展示页面
                {
                    result += @"<a href='{0}' class='cheak_score'>查看战绩</a>";
                    result = string.Format(result, singlepaihangUrl);
                }
                else
                {
                    result += @"<a href='{0}' class='playtooBtn'>我也要玩</a>";
                    result = string.Format(result, indexUrl);
                }
            }

            #endregion

            return result;
        }

        public bool AwardTip { get; set; }

        private void InitAwardTip()
        {
            var awardUser = RelayQuery.GetActivityAwardUser(SessionHelp.UserData.WeixinUserId);
            if (awardUser != null)
            {
                if (string.IsNullOrEmpty(awardUser.name))
                {
                    AwardTip = true;
                    return;
                }
            }
            AwardTip = false;
        }
    }
}